<?php
/**
 * Created by PhpStorm.
 * User: EDZ
 * Date: 2020/6/22
 * Time: 11:01
 */

$prefixList = [
    'DesignPatterns\\Structural\\Adaptor',
    'DesignPatterns\\Creative\\AbstractFactory',
    'DesignPatterns\\Behavioral\\Template',
    'DesignPatterns\\Behavioral\\Strategy',
    'DesignPatterns\\Behavioral\\Observer',
    'utils'
];

foreach ($prefixList as $prefix) {
    // clazz "DesignPatterns\Structural\Adaptor\Book" remove ".php"
    // prefix "DesignPatterns\Structural\Adaptor"
    spl_autoload_register(function($clazz) use($prefix) {
        $len = strlen($prefix);
        if (0 != strncmp($prefix, $clazz, $len)) {
            return;
        }
        $baseDir = dirname(__FILE__) . DIRECTORY_SEPARATOR.
            str_replace('\\', DIRECTORY_SEPARATOR, $prefix);
        $relativeClass = substr($clazz, $len);

        $file = $baseDir.str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass).'.php';
        if (!file_exists($file)) {
            throw new \InvalidArgumentException($file.' does not exists');
        }
        require $file;
    });
}